import wx
from panel import *
from log_object import *

class wxBufferedWindow(wx.Window):

	def __init__(self, parent, id, pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.NO_FULL_REPAINT_ON_RESIZE):
		wx.Window.__init__(self, parent, id, pos, size, style)
		#wx.SplitterWindow.__init__(self, parent, id, pos, size, style)
		self.Width = 0
		self.Height = 0

		self.sideTabSize = 340 
		self.CLOSEFLAG = 0 

		self.WindowUpdate = 0
		wx.EVT_PAINT(self, self.OnPaint)
		wx.EVT_SIZE(self, self.OnSize)

		self.OnSize(None)

	def Draw(self,dc):
		## just here as a place holder.
		## This method should be over-ridden when sub-classed
		pass

	def OnPaint(self, event):
		# All that is needed here is to draw the buffer to screen
		dc = wx.PaintDC(self)
		dc.DrawBitmap(self._Buffer,0,0)

	def OnSize(self,event):
		self.Width, self.Height = self.GetClientSizeTuple()
				
		if self.CLOSEFLAG == 1 :
			self.Width = self.Width - 45 
		else :
			self.Width = self.Width - self.sideTabSize
		self.WindowUpdate =1

		if self.Width > 0 and self.Height > 0 :
			if self._Buffer != None : 
				del self._Buffer
			self._Buffer = wx.EmptyBitmap(self.Width, self.Height)
			self.UpdateDrawing()
		

	def UpdateDrawing(self):
		# update the buffer
		dc = wx.MemoryDC()
		dc.SelectObject(self._Buffer)
		
		self.Draw(dc)
		# update the screen
		wx.ClientDC(self).Blit(0, 0, self.Width, self.Height, dc, 0, 0)
		


	def SaveDrawing(self, name):
		self.UpdateDrawing()
		self._Buffer.SaveFile("./output/"+name+".tiff", wx.BITMAP_TYPE_TIF)



class DataCanvas(wxBufferedWindow):
	def __init__(self, parent, winsize, id = -1):
		self.bitmaps = {}
		self.fg = wx.Colour(255,255,255)
		self.parent = parent
		self.start_time = 0 
		self.end_time =  -1

		wxBufferedWindow.__init__(self, parent, id)
		self.Width = winsize[0] 
		self.Height = winsize[1]
		print "size...", self.Width, self.Height
		self._Buffer = wx.EmptyBitmap(self.Width, self.Height)

		self.sidePanel = wx.Panel(self, -1)
		self.sidePanel.SetBackgroundColour(wx.Colour(255, 255, 255))

		self.sideNote = wx.Notebook(self.sidePanel, -1, style=wx.NB_RIGHT | wx.NB_MULTILINE)
		self.sideNote.SetBackgroundColour(wx.Colour(255, 255, 255))

		self.dmPanel = wx.Panel(self.sideNote, -1, (0, 50), (45, 500), style=wx.NO_BORDER )
		self.dmPanel.Hide()

		self.closePanel = wx.Panel(self.sideNote, -1, (0, 50), (45, 500), style=wx.NO_BORDER )
		self.closePanel.Hide()

		self.userPanel = wx.Panel(self.sideNote, -1, (0, 50), (300, 500), style=wx.NO_BORDER )
		self._userPanel = ListPanel(self.parent, self.userPanel, "Participants")
		self.userPanel.Hide()

		self.casePanel = wx.Panel(self.sideNote, -1, (0, 50), (300, 500), style=wx.NO_BORDER )
		self._casePanel = ListPanel(self.parent, self.casePanel, "Case")
		self.casePanel.Hide()

		self.graphPanel = wx.Panel(self.sideNote, -1, (0, 50), (300, 500), style=wx.NO_BORDER )
		self._graphPanel = GraphPanel(self.parent, self.graphPanel)
		self.graphPanel.Show()

		self.notePanel = wx.Panel(self.sideNote, -1, (0, 50), (300, 500), style=wx.NO_BORDER )
		self._notePanel = ScriptPanel(self.parent, self.notePanel)
		self.notePanel.Hide()

		self.sideNote.AddPage(self.userPanel, 'Participant')
		self.sideNote.AddPage(self.casePanel, 'Case')
		self.sideNote.AddPage(self.graphPanel, 'Graph')
		self.sideNote.AddPage(self.notePanel, 'Note/Script')
		self.sideNote.AddPage(self.dmPanel, 'Go to DM Window')
		self.sideNote.AddPage(self.closePanel, 'Close')

		self.sideNote.SetSelection(2)
		self.prevSelect =2 

		self.Width = self.Width - self.sideTabSize
		self.sidePanel.SetPosition((self.Width, 0))
		self.sidePanel.SetSize((self.sideTabSize,self.Height))
		self.sideNote.SetSize((self.sideTabSize,self.Height))
                self.sideNote.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.__onSelectNote)

	def closeTab(self):
		self.sideNote.SetSelection(5)

	def clearPanel(self):
		self.start_time = 0 
		self.end_time =  -1
		self._userPanel.clearPanel()
		self._casePanel.clearPanel()
		self._graphPanel.clearPanel()
		self.sideNote.SetSelection(2)

	def addParcitipant(self, name): 
		self._userPanel.addInData(name)

	def addCase(self, name): 
		self._casePanel.addInData(name)

	def  getTagList(self):
		return self._graphPanel.getTagList()

	def __window_update__(self):
		self.Width, self.Height = self.parent.GetClientSizeTuple()
		self.SetSize((self.Width,self.Height))

		tab = self.sideTabSize
		if self.CLOSEFLAG == 1:
			tab = 45

		#self.parent.UpdateSize(self.Width, self.Height)
		self.Width = self.Width - tab
		self.sidePanel.SetPosition((self.Width, 0))
		self.sidePanel.SetSize((tab,self.Height))
		self.sideNote.SetSize((tab,self.Height))

	def __onSelectNote(self, event):
		note_id = event.GetSelection()
		if note_id == 5:
			self.CLOSEFLAG = 1
		else :		
			self.CLOSEFLAG = 0 
			if note_id == 4:
				self.parent.showDataManager()
				self.parent.clearPanel()
		
		if self.prevSelect == 5 or note_id == 5:
			self.__window_update__()
			self._Buffer = wx.EmptyBitmap(self.Width, self.Height)
			self.UpdateDrawing()

		self.prevSelect = note_id 
		event.Skip()

	def Draw(self, dc):
		dc.BeginDrawing()
		dc.SetBackground(wx.Brush(self.parent.getBackgroundColor()))
		dc.Clear() 

		if self.WindowUpdate == 1 :
			self.__window_update__()
			self.WindowUpdate =0

		self.parent.graph_mgr.draw(dc, self.start_time, self.end_time)

	def setDisplayTimeRange(self, start, end):
		self.start_time = start 
		self.end_time = end 
		self.UpdateDrawing()

